<template>
  <el-dialog :close-on-click-modal="false" :title="isEdit ? '修改' : '新增'" :visible.sync="visible">
    <el-form ref="form" :model="form" label-width="150px" :rules="rules">
      <el-form-item label="考评项目父级">
        <el-select clearable v-model="form.parentId" style="width: 100%">
          <el-option
            v-for="(item,index) in parentArr"
            :key="index"
            :value="item.evalTypeId"
            :label="item.evalTypeName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="考评项目分类名称" prop="evalTypeName">
        <el-input v-model="form.evalTypeName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="负责人" prop="managerId">
        <dept-user
          ref="deptUser"
          :selectValue="form.managerId"
          @select="value => handleTreeSelect('managerId', value)"
        />
        <!-- <el-input v-model="form.managerId" autocomplete="off"></el-input> -->
      </el-form-item>
      <el-form-item label="排序" prop="evalTypeOrder">
        <el-input-number v-model="form.evalTypeOrder"></el-input-number>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button :loading="loading" type="primary" @click="handleOk">{{ isEdit ? '修 改' : '新 增' }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { create, update, getEvalParent } from '@/api/eval'
import { openNotification } from '@/utils/util'
import DeptUser from '@/components/DeptUserS'

export default {
  components: {
    DeptUser
  },
  data() {
    return {
      loading: false,
      form: {
        parentId: null,
        evalTypeId: null,
        evalTypeName: null,
        evalTypeOrder: 0,
        managerId: null,
        deptId: null
      },
      parentArr: [],
      rules: {
        evalTypeName: [{ required: true, message: '请输入考评项目分类名称', trigger: 'blur' }],
        managerId: [{ required: true, message: '请输入管理人', trigger: 'blur' }],
        evalTypeOrder: [
          { required: true, message: '请输入排序值', trigger: 'blur' },
          { type: 'number', message: '排序值为数字' }
        ]
      },
      visible: false,
      mdl: {},
      isEdit: false
    }
  },
  mounted() {
    getEvalParent().then(res => {
      let arr = res.data
      // console.log('parent res', res)
      this.parentArr = arr.filter(v => v.parentId === null)
    })
  },
  methods: {
    handleTreeSelect(key, value) {
      if (value.children && value.children.length > 0) {
        this.$message.error('请选择部门下的人员')
        this.$nextTick(() => {
          this.$set(this.form, key, null)
          this.$refs.deptUser.reset()
        })
      } else {
        // let id = value.id.split('-')[1]
        // let deptId = value.id.split('-')[0]

        // console.log('key', key, value.id, id)

        this.$set(this.form, key, value.id)
        // this.$set(this.form, 'deptId', deptId)
      }
    },
    edit(bool, row) {
      console.log('bool', bool, row)
      this.isEdit = bool
      this.visible = true
      this.form = Object.assign({}, row)
      if (!bool) {
        this.$nextTick(() => {
          this.$set(this.form, 'managerId', null)
          this.$set(this.form, 'evalTypeOrder', 0)
          this.$refs.deptUser.reset()
        })
        row.evalTypeOrder = 0
      } else {
        // 编辑
        this.$set(this.form, 'managerId', row.deptId + '-' + row.managerId)
      }

      console.log('form', this.form)
    },
    handleOk() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          this.loading = true

          let id = this.form.managerId.split('-')[1]
          let deptId = this.form.managerId.split('-')[0]
          this.$set(this.form, 'managerId', id)
          this.$set(this.form, 'deptId', deptId)

          let res = ''
          let mes = ''
          if (this.isEdit) {
            // 修改
            res = await update(this.form)
            mes = '修改操作成功'
          } else {
            // 新增
            res = await create(this.form)
            mes = '新增操作成功'
          }
          openNotification('success', mes)
          this.loading = false
          this.visible = false
          this.$emit('refresh')
        }
      })
    }
  }
}
</script>